package com.yjhh.tilemap.services;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yjhh.tilemap.domain.MapMarkInfo;
import com.yjhh.tilemap.repositories.MapMarkInfoRepository;
import com.yjhh.tilemap.utils.FileTools;
import com.yjhh.tilemap.utils.MapTools;
import com.yjhh.tilemap.utils.Polygon2D;
import com.yjhh.tilemap.utils.ResponseBean;
import com.yjhh.tilemap.vo.MapAreaInfo;
import com.yjhh.tilemap.vo.MapInfo;
import com.yjhh.tilemap.vo.TileMapInfo;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.broadinstitute.hdf5.HDF5File;
import org.geojson.*;
import org.geojson.Point;
import org.geojson.Polygon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class MapMarkInfoServiceImpl implements MapMarkInfoService {

    @Autowired
    private MapMarkInfoRepository mapMarkInfoRepository;

    @Value("${tilemap.dir.src}")
    private String tilemapSrcDir;

    @Value("${tilemap.dir}")
    private String tilemapDir;

    @Value("${tilemap.tempdir}")
    private String tilemapTempDir;

    @Value("${tilemap.zoom}")
    private String tilemapZoom;

    @Value("${tilemap.stitching.dir}")
    private String stitchingMapDir;

    @Value("${tilemap.hdf5.file.path}")
    private String tilemapHdf5FilePath;

    @Value("${tilemap.hdf5.file.name}")
    private String tilemapTdf5FileName;

    @Value("${tilemap.hdf5.file.extension}")
    private String tilemapHdf5FileExtension;

    @Autowired
    private ResourceLoader resourceLoader;

    @Override
    public ResponseBean<List<MapMarkInfo>> listAllMark() {
        Iterable<MapMarkInfo> all = mapMarkInfoRepository.findAll();
        return new ResponseBean("200", "ok", all);
    }

    @Override
    public ResponseBean<List<MapMarkInfo>> getMarkByPoint(Double lng, Double lat) throws IOException {
        //1.先找根据锁定经纬度所在矩形区域的Mark
        //2.然后根据找到的Mark中的geojson锁定具体的Mark
        List<MapMarkInfo> mapMarkInfoByArea = mapMarkInfoRepository.findMapMarkInfoByArea(lng, lat);

        List<MapMarkInfo> resultList = new ArrayList<>();
        for (MapMarkInfo mapMarkInfo : mapMarkInfoByArea) {
            String geoJson = mapMarkInfo.getGeoJson();
            GeoJsonObject object = new ObjectMapper().readValue(geoJson, GeoJsonObject.class);
            if (object instanceof Polygon) {
                Polygon polygon = (Polygon) object;
                boolean contains = checkPolygonIsContainsPoint(lng, lat, polygon);
                if (contains) {
                    resultList.add(mapMarkInfo);
                }
            } else if (object instanceof MultiPolygon) {

            } else if (object instanceof Point) {

            } else if (object instanceof MultiPoint) {

            } else if (object instanceof LineString) {

            } else if (object instanceof MultiLineString) {

            } else if (object instanceof Feature) {

            } else if (object instanceof FeatureCollection) {
                FeatureCollection featureCollection = (FeatureCollection) object;
                List<Feature> features = featureCollection.getFeatures();
                for (Feature feature : features) {
                    GeoJsonObject geometry = feature.getGeometry();
                    if (geometry instanceof Polygon) {
                        Polygon polygon = (Polygon) geometry;
                        boolean contains = checkPolygonIsContainsPoint(lng, lat, polygon);
                        if (contains) {
                            resultList.add(mapMarkInfo);
                        }
                    }
                }
            }
        }
        return new ResponseBean("200", "ok", resultList);
    }

    /**
     * 检测经纬度是否在多边形范围内
     *
     * @param lng
     * @param lat
     * @param polygon
     * @return
     */
    private boolean checkPolygonIsContainsPoint(Double lng, Double lat, Polygon polygon) {
        List<List<LngLatAlt>> coordinatesList = polygon.getCoordinates();

        Polygon2D polygon2d = new Polygon2D();

        for (List<LngLatAlt> coordinates : coordinatesList) {
            for (LngLatAlt coordinate : coordinates) {
                polygon2d.addPoint(new Point2D.Double(coordinate.getLongitude(), coordinate.getLatitude()));
            }
        }

        return polygon2d.contains(lng, lat);
    }

    @Override
    public ResponseBean startDrawMark() throws IOException {
        //绘制前先清空瓦片文件夹
        cleanAndCreateFolder(tilemapDir);
        cleanAndCreateFolder(tilemapTempDir);
        cleanAndCreateFolder(stitchingMapDir);

        //先用findAll找到所有的标记数据，以后改成分页的形式来查询，以免数据量
        Iterable<MapMarkInfo> markInfos = mapMarkInfoRepository.findAll();
        for (MapMarkInfo mapMarkInfo : markInfos) {
            String geoJson = mapMarkInfo.getGeoJson();

            GeoJsonObject object = new ObjectMapper().readValue(geoJson, GeoJsonObject.class);
            if (object instanceof Polygon) {
                Polygon polygon = (Polygon) object;

            } else if (object instanceof MultiPolygon) {

            } else if (object instanceof Point) {

            } else if (object instanceof MultiPoint) {

            } else if (object instanceof LineString) {

            } else if (object instanceof MultiLineString) {

            } else if (object instanceof Feature) {

            } else if (object instanceof FeatureCollection) {
                FeatureCollection featureCollection = (FeatureCollection) object;
                List<Feature> features = featureCollection.getFeatures();
                List<Polygon> polygonList = new ArrayList<>();
                //找到绘图范围
                MapAreaInfo mapAreaInfo = findDrawArea(features, polygonList);

                //转化为取左上角，右下角为矩形的金纬度，以便计算瓦片
                Double startLongitude = mapAreaInfo.getMinLng();
                Double startLatitude = mapAreaInfo.getMaxLat();
                Double endLongitude = mapAreaInfo.getMaxLng();
                Double endLatitude = mapAreaInfo.getMinLat();

                String[] zoomArray = StringUtils.split(tilemapZoom, ",");
                for (String zoomStr : zoomArray) {
                    int zoom = Integer.parseInt(zoomStr);

                    TileMapInfo startTileMapInfo = MapTools.getTileMapInfo(startLatitude, startLongitude, zoom);
                    TileMapInfo endTileMapInfo = MapTools.getTileMapInfo(endLatitude, endLongitude, zoom);

                    //拼装大图并绘制图形
                    File outFile = stitchingMapAndDraw(mapMarkInfo, polygonList, zoom, startTileMapInfo, endTileMapInfo, mapAreaInfo);

                    //----------对拼装并绘制好的图形进行拆分成万片-------
                    piecesMapToTile(zoom, startTileMapInfo, outFile);
                }
            }
        }

        //完成后复制所有的源地图瓦片，到定义好瓦片文件夹
        FileUtils.copyDirectory(new File(tilemapSrcDir), new File(tilemapDir));
        FileUtils.copyDirectory(new File(tilemapTempDir), new File(tilemapDir));

        return new ResponseBean("200", "ok");
    }

    private void cleanAndCreateFolder(String folderPath) throws IOException {
        File tilemapDirFile = new File(folderPath);
        if (tilemapDirFile.exists()) {
            FileUtils.cleanDirectory(tilemapDirFile);
        } else {
            tilemapDirFile.mkdirs();
        }
    }

    private void piecesMapToTile(int zoom, TileMapInfo startTileMapInfo, File outFile) throws IOException {
        BufferedImage outImage = ImageIO.read(outFile);
        int nCols = outImage.getWidth() / 256;
        int nRows = outImage.getHeight() / 256;

        BufferedImage outTileImage = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB_PRE);
        Graphics2D g = outTileImage.createGraphics();

        for (int i = 0; i < nRows; i++) {
            for (int j = 0; j < nCols; j++) {
                BufferedImage tempImage = outImage.getSubimage(j * 256, i * 256, 256, 256);
                g.drawImage(tempImage, 0, 0, 256, 256, null);

                String filePath = tilemapTempDir + "/satellite/" + zoom + "/" + (startTileMapInfo.getXtile() + j) + '/';
                File file = new File(filePath);
                if (!file.exists()) {
                    file.mkdirs();
                }
                File outTileFile = new File(filePath + (startTileMapInfo.getYtile() + i) + ".jpg");

                try {
                    ImageIO.write(outTileImage, "png", outTileFile);
                } catch (IOException e) {
                    System.err.println(e);
                    System.exit(1);
                }
            }
        }
    }

    private File stitchingMapAndDraw(MapMarkInfo mapMarkInfo, List<Polygon> polygonList, int zoom, TileMapInfo startTileMapInfo, TileMapInfo endTileMapInfo, MapAreaInfo mapAreaInfo) throws IOException {
        int cols = endTileMapInfo.getXtile() - startTileMapInfo.getXtile() + 1;
        int rows = endTileMapInfo.getYtile() - startTileMapInfo.getYtile() + 1;
        BufferedImage[][] images = new BufferedImage[cols][rows];

        for (int i = startTileMapInfo.getXtile(); i <= endTileMapInfo.getXtile(); i++) {
            for (int j = startTileMapInfo.getYtile(); j <= endTileMapInfo.getYtile(); j++) {
                String filePartPath = "/satellite/" + zoom + "/" + i + "/" + j + ".jpg";
                //检测已经绘制好的瓦片文件夹中是否存在此文件，如果存在则使用已经绘制好的瓦片，客养可以防止，漏掉已绘制的图形
                String tileTempFilePath = tilemapTempDir + filePartPath;
                String tileFilePath = tilemapSrcDir + filePartPath;
                if (new File(tileTempFilePath).exists()) {
                    tileFilePath = tileTempFilePath;
                }
                images[i - startTileMapInfo.getXtile()][j - startTileMapInfo.getYtile()] = ImageIO.read(new File(tileFilePath));
            }
        }

        BufferedImage outputImage = new BufferedImage(256 * cols, 256 * rows, BufferedImage.TYPE_INT_ARGB_PRE);
        Graphics2D graphics2D = outputImage.createGraphics();

        for (int i = 0; i < cols; i++) {
            for (int j = 0; j < rows; j++) {
                graphics2D.drawImage(images[i][j], i * 256, j * 256, 256, 256, null);
            }
        }

        //-------------拼装并绘制图像-----------
        int startXPixel = startTileMapInfo.getXtile() * 256;
        int startYPixel = startTileMapInfo.getYtile() * 256;

        //绘制geojson中的形状
        List<Integer> lngList = new ArrayList<>();
        List<Integer> latList = new ArrayList<>();
        for (Polygon polygon : polygonList) {
            List<List<LngLatAlt>> coordinates = polygon.getCoordinates();
            for (List<LngLatAlt> coordinateList : coordinates) {
                for (LngLatAlt lngLatAlt : coordinateList) {
                    lngList.add((int) MapTools.lngToPixel(lngLatAlt.getLongitude(), zoom) - startXPixel);
                    latList.add((int) MapTools.latToPixel(lngLatAlt.getLatitude(), zoom) - startYPixel);
                }
            }
        }

        //转化为int数组
        int[] lngArray = new int[lngList.size()];
        int[] latArray = new int[latList.size()];
        for (int i = 0; i < lngList.size(); i++) {
            lngArray[i] = lngList.get(i);
            latArray[i] = latList.get(i);
        }


        //平滑图形边缘,绘制矩形区域
        //检测标记图片是否存在，如果存在则不需要绘制区域线
        //绘制图片
        String markType = mapMarkInfo.getMarkType();
        if (StringUtils.isNotBlank(markType)) {
            Resource iconResource = resourceLoader.getResource("classpath:/markIcon/" + markType + ".png");
            if (iconResource != null) {
                File iconResourceFile = null;
                try {
                    iconResourceFile = iconResource.getFile();
                } catch (FileNotFoundException e) {
                    iconResourceFile = null;
                }
                if (iconResourceFile == null) {
                    graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                    int rgba = 0x80ABCDEF;
                    if (mapMarkInfo.getRgba() != null) {
                        rgba = mapMarkInfo.getRgba().intValue();
                    }
                    graphics2D.setColor(new Color(rgba, true));
                    graphics2D.fillPolygon(lngArray, latArray, latList.size());

                    BasicStroke bs = new BasicStroke(3, BasicStroke.CAP_BUTT,
                            BasicStroke.JOIN_BEVEL, 0,
                            new float[]{16, 4}, 0);
                    graphics2D.setStroke(bs);
                    graphics2D.setColor(new Color(13, 6, 255));
                    graphics2D.drawPolygon(lngArray, latArray, latList.size());
                }else{
                    //绘制图片
                    BufferedImage iconBufferedImage = ImageIO.read(iconResourceFile);

                    //转化为取左上角，右下角为矩形的金纬度，以便计算瓦片
                    Double startLongitude = mapAreaInfo.getMinLng();
                    Double startLatitude = mapAreaInfo.getMaxLat();
                    Double endLongitude = mapAreaInfo.getMaxLng();
                    Double endLatitude = mapAreaInfo.getMinLat();

                    //需要计算区域的大小，还需要对icon进行缩放
                    int startX = (int) MapTools.lngToPixel(startLongitude, zoom) - startXPixel;
                    int startY = (int) MapTools.latToPixel(startLatitude, zoom) - startYPixel;
                    int endX = (int) MapTools.lngToPixel(endLongitude, zoom) - startXPixel;
                    int endY = (int) MapTools.latToPixel(endLatitude, zoom) - startYPixel;

                    int areaWidth = endX - startX;
                    int areaHeight = endY - startY;

                    Image scaledInstance = iconBufferedImage.getScaledInstance(areaWidth, areaHeight, Image.SCALE_DEFAULT);

                    graphics2D.drawImage(scaledInstance,
                            startX,
                            startY,
                            scaledInstance.getWidth(null), scaledInstance.getHeight(null), null);
                }
            }
        }

        File parentFile = new File(stitchingMapDir);
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        File outFile = new File(stitchingMapDir + "/" + mapMarkInfo.getId() + "map" + zoom + ".png");
        ImageIO.write(outputImage, "png", outFile);
        return outFile;
    }

    /**
     * #80FFFFFF   50%透明白色
     * 100% — FF
     * 95% — F2
     * 90% — E6
     * 85% — D9
     * 80% — CC
     * 75% — BF
     * 70% — B3
     * 65% — A6
     * 60% — 99
     * 55% — 8C
     * 50% — 80
     * 45% — 73
     * 40% — 66
     * 35% — 59
     * 30% — 4D
     * 25% — 40
     * 20% — 33
     * 15% — 26
     * 10% — 1A
     * 5% — 0D
     * 0% — 00
     */

    private MapAreaInfo findDrawArea(List<Feature> features, List<Polygon> polygonList) {
        MapAreaInfo mapAreaInfo = null;
        for (Feature feature : features) {
            GeoJsonObject geometry = feature.getGeometry();
            if (geometry instanceof Polygon) {
                Polygon polygon = (Polygon) geometry;
                polygonList.add(polygon);
                List<List<LngLatAlt>> coordinates = polygon.getCoordinates();
                for (List<LngLatAlt> coordinateList : coordinates) {
                    for (LngLatAlt lngLatAlt : coordinateList) {
                        double longitude = lngLatAlt.getLongitude();   //经度
                        double latitude = lngLatAlt.getLatitude();     //纬度
                        //第一次赋值
                        if (mapAreaInfo == null) {
                            mapAreaInfo = new MapAreaInfo(longitude, latitude, longitude, latitude);
                        } else {
                            mapAreaInfo.setMinLng(Math.min(mapAreaInfo.getMinLng(), longitude));
                            mapAreaInfo.setMaxLng(Math.max(mapAreaInfo.getMaxLng(), longitude));
                            mapAreaInfo.setMinLat(Math.min(mapAreaInfo.getMinLat(), latitude));
                            mapAreaInfo.setMaxLat(Math.max(mapAreaInfo.getMaxLat(), latitude));
                        }
                    }
                }
            }
        }
        return mapAreaInfo;
    }

    @Override
    public ResponseBean<MapInfo> showMapInfo(int zoom, double startLng, double startLat, double endLng, double endLat, String serverPath) {
        if (zoom != 13 && zoom != 15 && zoom != 17 && zoom != 19) {
            return new ResponseBean("200", "not support " + zoom + " zoom");
        }

        TileMapInfo startTileMapInfo = MapTools.getTileMapInfo(startLat, startLng, zoom);
        TileMapInfo endTileMapInfo = MapTools.getTileMapInfo(endLat, endLng, zoom);

        double minLng = Math.min(startLng, endLng);
        double minLat = Math.min(startLat, endLat);
        double maxLng = Math.max(startLng, endLng);
        double maxLat = Math.max(startLat, endLat);

        List<MapMarkInfo> mapMarkInfoList = mapMarkInfoRepository.findMapMarkInfoByArea(minLng, minLat, maxLng, maxLat);

        ArrayList<String> tileMapUrlList = new ArrayList<>();
        for (int i = startTileMapInfo.getXtile(); i <= endTileMapInfo.getXtile(); i++) {
            for (int j = startTileMapInfo.getYtile(); j <= endTileMapInfo.getYtile(); j++) {
                tileMapUrlList.add(serverPath + "satellite/" + zoom + "/" + i + "/" + j + ".jpg");
            }
        }

        int xTileCount = endTileMapInfo.getXtile() - startTileMapInfo.getXtile() + 1;
        int yTileCount = endTileMapInfo.getYtile() - startTileMapInfo.getYtile() + 1;
        int totalTileCount = xTileCount * yTileCount;

        return new ResponseBean("200", "ok", new MapInfo(zoom, startLng, startLat, endLng, endLat, xTileCount, yTileCount, totalTileCount, mapMarkInfoList, tileMapUrlList));
    }

    @Override
    public void exportMarkToHDF5(HttpServletResponse response) throws IOException {
        //创建hdf5文件
        File hdfFilePath = new File(tilemapHdf5FilePath);
        if (!hdfFilePath.exists()) {
            hdfFilePath.mkdirs();
        }

        //检测文件是否存在，如果存在则直接返回文件
        File outFile = new File(tilemapHdf5FilePath + "/" + tilemapTdf5FileName + tilemapHdf5FileExtension);
        HDF5File file = null;
        if (!outFile.exists()) {
            file = new HDF5File(outFile, HDF5File.OpenMode.CREATE);
            String groupPath = "map/tilemap";
            file.makeGroup(groupPath);

            //查找所有级别地图瓦片，将其信息生产HDF5文件
            String satelliteMapDir = tilemapDir + "/satellite";
            File satelliteMapFolder = new File(satelliteMapDir);
            if (!satelliteMapFolder.exists()) {
                return;
            }

            //对文件夹的名称进程从小到大的排序
            File[] zoomFolders = satelliteMapFolder.listFiles();
            List<File> zoomFolderList = Arrays.asList(zoomFolders);
            FileTools.sortByFileName(zoomFolderList);


            //检测瓦片是否和地图标记点区域有交集
            Iterable<MapMarkInfo> mapMarkInfos = mapMarkInfoRepository.findAll();
            for (File zoomFolder : zoomFolderList) {
                String zoomFolderName = zoomFolder.getName();
                String zoomGroup = groupPath + "/" + zoomFolderName;
                file.makeGroup(zoomGroup);
                List<File> xIndexFileList = Arrays.asList(zoomFolder.listFiles());
                FileTools.sortByFileName(xIndexFileList);
                for (File xIndexFile : xIndexFileList) {
                    List<File> yIndexFileList = Arrays.asList(xIndexFile.listFiles());
                    FileTools.sortByFileName(yIndexFileList);
                    for (File yIndexFile : yIndexFileList) {
                        String yIndexFileName = yIndexFile.getName();
                        String xIndex = xIndexFile.getName();
                        String yIndex = yIndexFileName.substring(0, yIndexFileName.lastIndexOf("."));
                        String satelliteTileMap = "/satellite/" + zoomFolderName + "/" + xIndexFile.getName() + "/" + yIndexFileName;

                        List<MapMarkInfo> mapMarkInfoList = checkIntersects(Double.parseDouble(xIndex) * 256, Double.parseDouble(yIndex) * 256, Integer.parseInt(zoomFolderName), mapMarkInfos);

                        String attrPath = zoomGroup + "/" + xIndex + "_" + yIndex;
                        List<String> markNames = new ArrayList<>();
                        List<String> markIds = new ArrayList();
                        for (MapMarkInfo mapMarkInfo : mapMarkInfoList) {
                            markNames.add(mapMarkInfo.getMarkName());
                            markIds.add(mapMarkInfo.getId() + "");
                        }

                        final String[] testValues = new String[]{xIndex, yIndex, satelliteTileMap, mapMarkInfoList.size() <= 0 ? "0" : "1", StringUtils.join(markNames, "|"), StringUtils.join(markIds, "|")};
                        file.makeStringArray(attrPath, testValues);
                    }
                }
            }


            file.close();
        }

        //导出时有界面，可选择下载路径
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(new String(outFile.getName().getBytes("utf-8"), "utf-8"), "UTF-8"));
        response.setContentType("application/x-hdf");

        OutputStream out1 = null;
        InputStream in = null;

        try {
            in = new FileInputStream(outFile);

            out1 = response.getOutputStream();
            BufferedInputStream bis = new BufferedInputStream(in);
            BufferedOutputStream bos = new BufferedOutputStream(out1);

            byte[] buff = new byte[20480];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
            bis.close();
            bos.flush();
            bos.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out1 != null)
                out1.close();
            if (in != null)
                in.close();
            if (file != null) {
                file.close();
            }
        }
    }

    /**
     * 检测瓦片是否和地图标记点区域有交集
     *
     * @param startXPixel
     * @param startYPixel
     * @param zoom
     * @param mapMarkInfos
     * @return
     * @throws IOException
     */
    private List<MapMarkInfo> checkIntersects(double startXPixel, double startYPixel, int zoom, Iterable<MapMarkInfo> mapMarkInfos) throws IOException {
        List<MapMarkInfo> mapMarkInfoList = new ArrayList<>();
        for (MapMarkInfo mapMarkInfo : mapMarkInfos) {
            String geoJson = mapMarkInfo.getGeoJson();
            GeoJsonObject object = new ObjectMapper().readValue(geoJson, GeoJsonObject.class);
            if (object instanceof Polygon) {
            } else if (object instanceof MultiPolygon) {
            } else if (object instanceof Point) {
            } else if (object instanceof MultiPoint) {
            } else if (object instanceof LineString) {
            } else if (object instanceof MultiLineString) {
            } else if (object instanceof Feature) {
            } else if (object instanceof FeatureCollection) {
                FeatureCollection featureCollection = (FeatureCollection) object;
                List<Feature> features = featureCollection.getFeatures();
                List<Polygon> polygonList = new ArrayList<>();
                //找到绘图范围
                MapAreaInfo mapAreaInfo = findDrawArea(features, polygonList);

                //绘制geojson中的形状
                List<Integer> lngList = new ArrayList<>();
                List<Integer> latList = new ArrayList<>();
                for (Polygon polygon : polygonList) {
                    List<List<LngLatAlt>> coordinates = polygon.getCoordinates();
                    for (List<LngLatAlt> coordinateList : coordinates) {
                        for (LngLatAlt lngLatAlt : coordinateList) {
                            lngList.add((int) MapTools.lngToPixel(lngLatAlt.getLongitude(), zoom));
                            latList.add((int) MapTools.latToPixel(lngLatAlt.getLatitude(), zoom));
                        }
                    }
                }

                //转化为int数组
                int[] lngArray = new int[lngList.size()];
                int[] latArray = new int[latList.size()];
                for (int i = 0; i < lngList.size(); i++) {
                    lngArray[i] = lngList.get(i);
                    latArray[i] = latList.get(i);
                }
                java.awt.Polygon awtPolygon = new java.awt.Polygon(lngArray, latArray, lngList.size());
                boolean intersects = awtPolygon.intersects(startXPixel, startYPixel, 256, 256);
                if (intersects) {
                    mapMarkInfoList.add(mapMarkInfo);
                }
            }
        }
        return mapMarkInfoList;
    }
}
